using ERPBackend.Data;
using ERPBackend.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace ERPBackend.Services
{
    public class SupplierQualificationService : ISupplierQualificationService
    {
        private readonly ApplicationDbContext _context;

        public SupplierQualificationService(ApplicationDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public async Task<IEnumerable<SupplierQualification>> GetQualificationsBySupplierIdAsync(int supplierId)
        {
            return await _context.SupplierQualifications
                .Where(sq => sq.SupplierId == supplierId)
                .ToListAsync() ?? Enumerable.Empty<SupplierQualification>();
        }

        public async Task<SupplierQualification?> GetQualificationByIdAsync(int id)
        {
            return await _context.SupplierQualifications.FindAsync(id);
        }

        public async Task<SupplierQualification> CreateQualificationAsync(SupplierQualification qualification)
        {
            // 检查供应商是否存在
            var supplierExists = await _context.Suppliers
                .AnyAsync(s => s.Id == qualification.SupplierId);
            if (!supplierExists)
            {
                throw new ArgumentException("供应商不存在");
            }

            _context.SupplierQualifications.Add(qualification);
            await _context.SaveChangesAsync();
            return qualification;
        }

        public async Task<bool> UpdateQualificationAsync(SupplierQualification qualification)
        {
            // 检查资质是否存在
            var existingQualification = await _context.SupplierQualifications.FindAsync(qualification.Id);
            if (existingQualification == null)
            {
                return false;
            }

            // 检查供应商是否存在
            var supplierExists = await _context.Suppliers
                .AnyAsync(s => s.Id == qualification.SupplierId);
            if (!supplierExists)
            {
                throw new ArgumentException("供应商不存在");
            }

            // 更新属性
            existingQualification.QualificationType = qualification.QualificationType;
            existingQualification.FileName = qualification.FileName;
            existingQualification.FilePath = qualification.FilePath;
            existingQualification.UploadTime = qualification.UploadTime;
            existingQualification.ValidUntil = qualification.ValidUntil;
            existingQualification.Status = qualification.Status;

            _context.Entry(existingQualification).State = EntityState.Modified;
            await _context.SaveChangesAsync();
            return true;
        }

        public async Task<bool> DeleteQualificationAsync(int id)
        {
            var qualification = await _context.SupplierQualifications.FindAsync(id);
            if (qualification == null)
            {
                return false;
            }

            _context.SupplierQualifications.Remove(qualification);
            await _context.SaveChangesAsync();
            return true;
        }

        public async Task<bool> ChangeQualificationStatusAsync(int id, byte status)
        {
            var qualification = await _context.SupplierQualifications.FindAsync(id);
            if (qualification == null)
            {
                return false;
            }

            qualification.Status = status;
            _context.Entry(qualification).State = EntityState.Modified;
            await _context.SaveChangesAsync();
            return true;
        }
    }
}